home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / varia / interp18.lha / interp-1.8 / Ast.cc < prev    next >
C/C++ Source or Header  |  1990-01-20  |  5KB  |  249 lines

  1. // Ast.cc -- the definition of the member functions for class Ast.
  2. // Copyright (C) 1989 Carey Richard Murphey.
  3. // (rich@rice.edu) 5310 Rutherglenn, Houston, TX 77096
  4.  
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 1, or (at your option)
  8. // any later version.
  9.  
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14.  
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. #include "Ast.h"
  20.  
  21. #if !defined(__OPTIMIZE__) || defined(INLINE)
  22. #ifndef INLINE
  23. #define INLINE
  24. #endif
  25.  
  26. // class  Node
  27. INLINE friend ostream& operator << (ostream& Stream, Node* p)
  28. {
  29.   return p->echo (Stream);
  30. }
  31. INLINE Node:: operator double ()
  32. {
  33.   return this->eval();
  34. }
  35.  
  36. // class  NoOp
  37. INLINE double NoOp:: eval()
  38. {
  39.   return 0.;
  40. }
  41. INLINE ostream& NoOp:: echo (ostream& Stream) return r(Stream);
  42. {
  43.   return;
  44. }
  45.  
  46. // class  Sym
  47. INLINE double Sym:: eval()
  48. {
  49.   return (double) *(*table)[name];
  50. }
  51. INLINE Sym:: operator double ()
  52. {
  53.   return (double) *(*table)[name];
  54. }
  55. INLINE Sym:: operator int ()
  56. {
  57.   return (int) *(*table)[name];
  58. }
  59. INLINE double Sym:: func (double p)
  60. {
  61.   return (*table)[name]->func (p);
  62. }
  63. INLINE void Sym:: assign (double p)
  64. {
  65.   (*table)[name]->assign (p);
  66. }
  67. INLINE ostream& Sym:: echo (ostream& Stream)
  68. {
  69.   return Stream << name;
  70. }
  71.  
  72. // class  Func
  73. INLINE double Func:: eval()
  74. {
  75.   return left->func (right->eval ());
  76. }
  77. INLINE ostream& Func:: echo (ostream& Stream)
  78. {
  79.   return Stream << left << " (" << right << ")";
  80. }
  81.  
  82. // class  Assign
  83. INLINE double Assign:: eval ()
  84. {
  85.   left->assign (right->eval ());
  86.   return (double) *left;
  87. }
  88. INLINE ostream& Assign:: echo (ostream& Stream)
  89. {
  90.   return Stream << left << " = " << right;
  91. }
  92.  
  93. // class  PostIncrement
  94. INLINE double PostIncrement:: eval ()
  95. {
  96.   double t = (double) *left;
  97.   left->assign ( 1. + (double) *left);
  98.   return t;
  99. }
  100. INLINE ostream& PostIncrement:: echo (ostream& Stream)
  101. {
  102.   return Stream << left << "++";
  103. }
  104.  
  105. // class  PostDecrement
  106. INLINE double PostDecrement:: eval ()
  107. {
  108.   double t = (double) *left;
  109.   left->assign ( -1. + (double) *left);
  110.   return t;
  111. }
  112. INLINE ostream& PostDecrement:: echo (ostream& Stream)
  113. {
  114.   return Stream << left << "--";
  115. }
  116.  
  117. // class  PreIncrement
  118. INLINE double PreIncrement:: eval ()
  119. {
  120.   left->assign ( 1. + (double) *left);
  121.   return (double) *left;
  122. }
  123. INLINE ostream& PreIncrement:: echo (ostream& Stream)
  124. {
  125.   return Stream << "++" << left;
  126. }
  127.  
  128. // class  PreDecrement
  129. INLINE double PreDecrement:: eval ()
  130. {
  131.   left->assign ( -1. + (double) *left);
  132.   return (double) *left;
  133. }
  134. INLINE ostream& PreDecrement:: echo (ostream& Stream)
  135. {
  136.   return Stream << "--" << left;
  137. }
  138.  
  139. // class  If
  140. INLINE double If:: eval ()
  141. {
  142.   if(0.==left->eval()) return 0.;
  143.   return right->eval();
  144. }
  145. INLINE ostream& If:: echo (ostream& Stream)
  146. {
  147.   return Stream << "if (" << left << ") " << right;
  148. }
  149.  
  150. // class  While
  151. INLINE double While:: eval ()
  152. {
  153.   while (0.!=left->eval ()) right->eval();
  154.   return 0.;
  155. }
  156. INLINE ostream& While:: echo (ostream& Stream)
  157. {
  158.   return Stream << "while (" << left << ") " << right;
  159. }
  160.  
  161. // class  Statements
  162. INLINE double Statements:: eval ()
  163. {
  164.   left->eval ();
  165.   return right->eval ();
  166. }
  167. INLINE ostream& Statements:: echo (ostream& Stream)
  168. {
  169.   return Stream << "{" << left << "; " << right << ";}";
  170. }
  171.  
  172. // class  Plus
  173. INLINE double Plus:: eval ()
  174. {
  175.   return left->eval () + right->eval ();
  176. }
  177. INLINE ostream& Plus:: echo (ostream& Stream)
  178. {
  179.   return Stream << left << " + " << right;
  180. }
  181.  
  182. // class  Minus
  183. INLINE double Minus:: eval ()
  184. {
  185.   return left->eval () - right->eval ();
  186. }
  187. INLINE ostream& Minus:: echo (ostream& Stream)
  188. {
  189.   return Stream << left << " - " << right;
  190. }
  191.  
  192. // class  Times
  193. INLINE double Times:: eval ()
  194. {
  195.   return left->eval () * right->eval ();
  196. }
  197. INLINE ostream& Times:: echo (ostream& Stream)
  198. {
  199.   return Stream << left << " * " << right;
  200. }
  201.  
  202. // class  Divide
  203. INLINE double Divide:: eval ()
  204. {
  205.   double t = right->eval ();
  206.   if (t == 0.)
  207.     {
  208.       cerr << "Error: divide by zero.";
  209.       return 0.;
  210.     }
  211.   else return left->eval () / t;
  212. }
  213. INLINE ostream& Divide:: echo (ostream& Stream)
  214. {
  215.   return Stream << left << " / " << right;
  216. }
  217.  
  218. // class  Pow
  219. INLINE double Pow:: eval ()
  220. {
  221.   return pow (left->eval (), right->eval ());
  222. }
  223. INLINE ostream& Pow:: echo (ostream& Stream)
  224. {
  225.   return Stream << left << "^" << right;
  226. }
  227.  
  228. // class  Double
  229. INLINE double Double:: eval ()
  230. {
  231.   return value;
  232. }
  233. INLINE ostream& Double:: echo (ostream& Stream)
  234. {
  235.   return Stream << value;
  236. }
  237.  
  238. // class  Uminus
  239. INLINE double Uminus:: eval ()
  240. {
  241.   return - left->eval ();
  242. }
  243. INLINE ostream& Uminus:: echo (ostream& Stream)
  244. {
  245.   return Stream << "- " << left;
  246. }
  247.  
  248. #endif
  249.